package cn.zhc.com.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;

import java.util.List;

import cn.zhc.com.R;

/**
 * Created by zhouhongchun on 2017/8/14.
 */

public class UtilsOther {
    /**
     * 用于检查传入Context是否不为空，或者Activity是否存活
     * @param context
     * @return
     */
    public static boolean checkActivityRunning(Context context) {
        if (context == null) {
            return false;
        }
        if (context instanceof Activity){
            Activity activity = (Activity) context;
            if (activity.isFinishing()){
                return false;
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                if (activity.isDestroyed())
                    return true;
            }

        }

        return false;
    }
    /**
     * activity从下到上进入动画
     */
    public static void showActivityInAnim(Activity activity) {
        activity.overridePendingTransition(R.anim.activity_down_up_anim, R.anim.activity_exit_anim);
    }
    /**
     * activity从上到下退出动画
     */
    public static void showActivityExitAnim(Activity activity) {

        activity.overridePendingTransition(R.anim.activity_exit_anim, R.anim.activity_up_down_anim);

    }

    /**
     * 隐藏身份证号码
     * @param idCard
     * @return
     */
    public static String hideInformation(String idCard) {
//        return idCard.replaceAll("(\\d{4})\\d{16}(\\d{4})","$1****$2");
        if (idCard == "") {
            return "信息不完整，请重新申请认证";
        }
        return idCard.replaceAll("(\\d)\\d{16}(\\d)", "$1*****************$2");
    }

    public static void call(Activity curActivity, String number) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:" + number));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        curActivity.startActivity(intent);
    }


    /**
     * 检查app是否在前台运行
     * @return ture 表示app在前台运行
     */
    public static boolean checkAppRunningForeGroud(Context context, String packageName) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : runningAppProcesses) {
            if (runningAppProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
                    && runningAppProcessInfo.processName.equals(packageName)) {
                return true;
            }
        }
        return false;
    }
    /**
     * 获得手机类型.
     * @return 返回手机类型
     */
    public static String getDeviceMODEL() {
        return Build.MODEL;
    }


    /**
     * 获得操作系统的版本号.
     * @return 操作系统的版本号
     */
    public static String getOsVersion() {
        return Build.VERSION.RELEASE;
    }

    /**
     * 获得SDK版本号.
     * @return SDK版本号
     */
    public static int getSDKVersion() {
        return Build.VERSION.SDK_INT;
    }


    /**
     * 获取版本名
     * @return 当前应用的版本号
     */
    public static String getVersionName(Context context) {
        return getPackageInfo(context).versionName;
    }

    /**
     * 获取修订版本号
     * @return 当前应用的版本号
     */
    public static int getVersionCode(Context context) {
        return getPackageInfo(context).versionCode;
    }

    /**
     * @param context
     * @return App的名称
     */
    public static String getAPPName(Context context) {
        return getPackageInfo(context).applicationInfo.loadLabel(context.getPackageManager())+"";
    }
    /**
     * PackageInfo
     */
    public static PackageInfo getPackageInfo(Context context) {
        PackageInfo pi = null;
        try {
            PackageManager pm = context.getPackageManager();
            pi = pm.getPackageInfo(context.getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);

            return pi;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return pi;
    }

}
